<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="真实姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="参赛单元（1：短片 2：短视频）" prop="unit">
        <el-input v-model="dataForm.unit" placeholder="参赛单元（1：短片 2：短视频）"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="tel">
        <el-input v-model="dataForm.tel" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="电子邮件" prop="email">
        <el-input v-model="dataForm.email" placeholder="电子邮件"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="作品名称" prop="workName">
        <el-input v-model="dataForm.workName" placeholder="作品名称"></el-input>
      </el-form-item>
      <el-form-item label="作品简介" prop="workInfo">
        <el-input v-model="dataForm.workInfo" placeholder="作品简介"></el-input>
      </el-form-item>
      <el-form-item label="作品链接" prop="workLink">
        <el-input v-model="dataForm.workLink" placeholder="作品链接"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          unit: '',
          tel: '',
          email: '',
          address: '',
          workName: '',
          workInfo: '',
          workLink: ''
        },
        dataRule: {
          name: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '参赛单元（1：短片 2：短视频）不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮件不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          workName: [
            { required: true, message: '作品名称不能为空', trigger: 'blur' }
          ],
          workInfo: [
            { required: true, message: '作品简介不能为空', trigger: 'blur' }
          ],
          workLink: [
            { required: true, message: '作品链接不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.basework.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.baseWork.name
                this.dataForm.unit = data.baseWork.unit
                this.dataForm.tel = data.baseWork.tel
                this.dataForm.email = data.baseWork.email
                this.dataForm.address = data.baseWork.address
                this.dataForm.workName = data.baseWork.workName
                this.dataForm.workInfo = data.baseWork.workInfo
                this.dataForm.workLink = data.baseWork.workLink
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'unit': this.dataForm.unit,
              'tel': this.dataForm.tel,
              'email': this.dataForm.email,
              'address': this.dataForm.address,
              'workName': this.dataForm.workName,
              'workInfo': this.dataForm.workInfo,
              'workLink': this.dataForm.workLink
            }
            var tick = !this.dataForm.id ? API.basework.add(params) : API.basework.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
